﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace PJ.Data
{
    public abstract class EntityWCFProxyProvider<C, S, D, I> : ObjectWCFProxyProvider<C, S, D, I>, IEntityProvider<I>
        where I : class, IEntityData
        where D : BaseEntityData, I, new()
        where S : class, IEntityProvider<D>
        where C : EntityProviderClient<S, D>, new()
    {


        protected EntityWCFProxyProvider(IObjectDataCache<I> cache) : base(cache)
        {
        }

        public I GetById(string Id)
        {
            if (string.IsNullOrEmpty(Id))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.GetById(Id));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I FindById(string Id)
        {
            if (string.IsNullOrEmpty(Id))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.FindById(Id));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I CachedGetById(string Id)
        {
            if (string.IsNullOrEmpty(Id))
                throw new ArgumentNullException();
            try
            {
                I cache = Cache.FindById(Id);
                if (cache == null)
                    return Cache.Add(ClientBase.CachedGetById(Id));
                else
                    return cache;
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I CachedFindById(string Id)
        {
            if (string.IsNullOrEmpty(Id))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.CachedFindById(Id));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        #region Interface override
        IEntityData IEntityProvider.GetById(string id)
        {
            return GetById(id);
        }

        IEntityData IEntityProvider.CachedGetById(string id)
        {
            return CachedGetById(id);
        }

        IEntityData IEntityProvider.FindById(string id)
        {
            return GetById(id);
        }

        IEntityData IEntityProvider.CachedFindById(string id)
        {
            return GetById(id);
        }
        #endregion
        #region Protected

        new protected IEntityCache<I> Cache
        {
            get { return (IEntityCache<I>)base.Cache; }
        }

        #endregion
        
        #region Field

        
        #endregion
    }
}
